﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Quellenverwaltung.Meta;
using Quellenverwaltung.Threads;

namespace Quellenverwaltung.UI.WPF
{
    /// <summary>
    /// Interaktionslogik für FindJournal.xaml
    /// </summary>
    public partial class FindJournal : UserControl
    {
        public Quellenverwaltung.UI.Forms.JournalHost HostForm { get; set; }
        private ReadDOIProcess journalProcess;

        public FindJournal()
        {
            InitializeComponent();
        }

        private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            search();
        }

        private void textBoxSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            search();
        }

        private void search()
        {
            if (!String.IsNullOrEmpty(textBoxSearch.Text))
            {
                string uri = "";
                try
                {
                    uri = textBoxSearch.Text;
                    if (!uri.Contains("http://") && !uri.Contains("https://"))
                    {
                        uri = "http://dx.doi.org/" + uri;
                    }
                    textBoxSearch.Text = uri;
                    browser.Visibility = Visibility.Visible;
                    browser.Navigate(uri);

                    InfoBox.Visibility = Visibility.Collapsed;
                    EditJournal.Opacity = 100;
                    
                    EditJournal.Visibility = Visibility.Collapsed;
                    gridSearchingIndicator.Visibility = Visibility.Visible;

                    journalProcess = new ReadDOIProcess();
                    journalProcess.ResultFound += DOIProcess_ResultFound;

                    journalProcess.Start(uri);
                }
                catch
                {
                    MessageBox.Show("Die URL '" + uri + "' konnte nicht geladen werden");
                }
            }
        }

        void DOIProcess_ResultFound(object sender, DOIProcessArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (SendOrPostCallback)delegate
            {
                ((EditJournal)this.EditJournal).setJournalDataSource(e.Result);

                gridSearchingIndicator.Visibility = Visibility.Collapsed;
                EditJournal.Visibility = Visibility.Visible;
            }, null);
        }

        private void textBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
